package com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter;

import com.kanshu.ksgb.fastread.doudou.app.Xutils;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.BaseMvpPresenter;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.IGenrialMvpView;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.PageRequestParams;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper;
import com.kanshu.ksgb.fastread.doudou.common.net.rx.BaseObserver;
import com.kanshu.ksgb.fastread.doudou.common.util.NetUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.BannerItem;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.CategoryBean;
import com.kanshu.ksgb.fastread.doudou.module.book.retrofit.requestparams.BookListReqParams;
import com.kanshu.ksgb.fastread.doudou.module.book.retrofit.requestparams.SearchRequestParams;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.BookDetailsBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.BookSet;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.ItemRefreshBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedBannerBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedCategoryBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.WrapBookInfo;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.retrofit.BookCityService;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.retrofit.BookSetRequestParams;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.retrofit.SelectedCategoryReq;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.retrofit.SelectedRequestParams;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.retrofit.TopicBannerParams;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.utils.BookCityUtils;

import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.subjects.Subject;

/**
 * Created by wcy on 18/5/17.
 */

public class BookCityPresenter extends BaseMvpPresenter {

    IGenrialMvpView<BaseResult<List<SelectedBean>>> mMvpView;
    IGenrialMvpView<BaseResult<List<SelectedCategoryBean>>> mCategoryView;
    IGenrialMvpView<BaseResult<List<BookInfo>>> mBookView;
    IGenrialMvpView<BaseResult<BookDetailsBean>> mBookDetailsView;
    ISelectedView mSelectedView;

    BookCityService mBookCityService;

    public void setBookView(IGenrialMvpView v) {
        mBookView = v;
    }
    public void setBookDetailsView(IGenrialMvpView v) {
        mBookDetailsView = v;
    }

    public BookCityPresenter(Subject<Integer> lifecycler) {
        super(lifecycler);
        RetrofitHelper helper = RetrofitHelper.getInstance();
        mBookCityService = helper.createService(BookCityService.class);
    }

    public void setSelectedView(ISelectedView view) {
        mSelectedView = view;
    }

    public void setMvpView(IGenrialMvpView mvpView) {
        mMvpView = mvpView;
    }

    public void setCategoryView(IGenrialMvpView view) {
        mCategoryView = view;
    }

    /**
     * 获取精选页信息
     * @param params
     */
    public void getSelectedInfos(PageRequestParams params) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (mSelectedView != null) {
                mSelectedView.showError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mBookCityService.getSelectedInfos(params)
                .compose(asyncRequest())
                .subscribe(new BaseObserver<List<SelectedBean>>() {
            @Override
            public void onResponse(BaseResult<List<SelectedBean>> listBaseResult, List<SelectedBean> selectedBeans, Disposable disposable) {
                if (mSelectedView != null && BaseResult.isNotNull(listBaseResult)) {
                    mSelectedView.showContent(listBaseResult);
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mSelectedView != null) {
                    mSelectedView.showError(code, errDesc);
                }
            }
        });
    }

    /**
     * 获取精选页banner
     * @param params
     * @param callback
     */
    public void getSelectedBanners(SelectedRequestParams params, final INetCommCallback<SelectedBannerBean> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
            }
            return;
        }
        mBookCityService.getSelectedBanners(params)
                .compose(asyncRequest())
                .subscribe(new BaseObserver<List<SelectedBannerBean>>() {
            @Override
            public void onResponse(BaseResult<List<SelectedBannerBean>> listBaseResult, List<SelectedBannerBean> selectedBeans, Disposable disposable) {
                if (callback != null && BaseResult.isNotNull(listBaseResult)) {
                    if (!Utils.isEmptyList(selectedBeans)) {
                        callback.onResponse(selectedBeans.get(0));
                    }
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

    /**
     * 获取男频精选信息
     */
    public void getMaleSelectedInfos() {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (mMvpView != null) {
                mMvpView.showError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mBookCityService.getMaleSelectedInfos()
                .compose(asyncRequest())
                .subscribe(new BaseObserver<List<SelectedBean>>() {
            @Override
            public void onResponse(BaseResult<List<SelectedBean>> listBaseResult, List<SelectedBean> selectedBeans, Disposable disposable) {
                if (mMvpView != null && BaseResult.isNotNull(listBaseResult)) {
                    mMvpView.showContent(listBaseResult);
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mMvpView != null) {
                    mMvpView.showError(code, errDesc);
                }
            }
        });
    }

    /**
     * 获取女频精选信息
     */
    public void getFemaleSelectedInfos() {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (mMvpView != null) {
                mMvpView.showError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mBookCityService.getFemaleSelectedInfos()
                .compose(asyncRequest())
                .subscribe(new BaseObserver<List<SelectedBean>>() {
            @Override
            public void onResponse(BaseResult<List<SelectedBean>> listBaseResult, List<SelectedBean> selectedBeans, Disposable disposable) {
                if (mMvpView != null && BaseResult.isNotNull(listBaseResult)) {
                    mMvpView.showContent(listBaseResult);
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mMvpView != null) {
                    mMvpView.showError(code, errDesc);
                }
            }
        });
    }

    /**
     * 获取底部数据
     * @param params
     */
    public void getBottomContents(PageRequestParams params) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (mSelectedView != null) {
                mSelectedView.showBottomError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mBookCityService.getWanRenZhiXuanSelectedInfos(params)
                .compose(asyncRequest())
                .subscribe(new BaseObserver<List<SelectedBean>>() {
            @Override
            public void onResponse(BaseResult<List<SelectedBean>> listBaseResult, List<SelectedBean> selectedBeans, Disposable disposable) {
                if (mSelectedView != null && BaseResult.isNotNull(listBaseResult)) {
                    mSelectedView.showBottomContent(listBaseResult);
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mSelectedView != null) {
                    mSelectedView.showBottomError(code, errDesc);
                }
            }
        });
    }

    /**
     * 获取单个推荐位数据
     * @param item
     * @param callback
     */
    public void getSelectedItem(ItemRefreshBean item, INetCommCallback<SelectedBean> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        SelectedRequestParams params = new SelectedRequestParams();
        params.type_name = item.selectedItem.type;
        params.page = BookCityUtils.updateItemPage(item);
        int num = 6;
        if (item.selectedItem.page_info != null) {
            params.num = Integer.parseInt(item.selectedItem.page_info.num);
        } else {
            params.num = num;
        }
        mBookCityService.getSelectedItem(params)
                .compose(asyncRequest())
                .subscribe(new BaseObserver<List<SelectedBean>>() {
            @Override
            public void onResponse(BaseResult<List<SelectedBean>> result, List<SelectedBean> selectedBean, Disposable disposable) {
                if (callback != null) {
                    if (Utils.isEmptyList(selectedBean)) {
                        callback.onError(Constants.ErrCode.NoData, "nodata");
                    } else {
                        callback.onResponse(selectedBean.get(0));
                    }
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

    /**
     * 男频换分类
     * @param callback
     */
    public void getMaleSwitchCategory(INetCommCallback<SelectedBean> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mBookCityService.getMaleSwitchCategory()
                .compose(asyncRequest())
                .subscribe(new BaseObserver<List<SelectedBean>>() {
            @Override
            public void onResponse(BaseResult<List<SelectedBean>> listBaseResult, List<SelectedBean> items, Disposable disposable) {
                if (!Utils.isEmptyList(items)) {
                    if (callback != null) {
                        callback.onResponse(items.get(0));
                    }
                } else {
                    if (callback != null) {
                        callback.onError(Constants.ErrCode.NoData, "no data");
                    }
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

    /**
     * 女频换分类
     * @param callback
     */
    public void getFemaleSwitchCategory(INetCommCallback<SelectedBean> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mBookCityService.getFemaleSwitchCategory()
                .compose(asyncRequest())
                .subscribe(new BaseObserver<List<SelectedBean>>() {
            @Override
            public void onResponse(BaseResult<List<SelectedBean>> listBaseResult, List<SelectedBean> items, Disposable disposable) {
                if (!Utils.isEmptyList(items)) {
                    if (callback != null) {
                        callback.onResponse(items.get(0));
                    }
                } else {
                    if (callback != null) {
                        callback.onError(Constants.ErrCode.NoData, "no data");
                    }
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

    /**
     * 热门标签
     * @param item
     * @param callback
     */
    public void getHotLabel(ItemRefreshBean item, INetCommCallback<SelectedBean> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        SelectedRequestParams params = new SelectedRequestParams();
        params.type_name = item.type;
        if (item != null && item.selectedItem.page_info != null) {
            params.page = BookCityUtils.updateItemPage(item);
            params.num = Integer.parseInt(item.selectedItem.page_info.num);
        }
        mBookCityService.getHotLabel(params)
                .compose(asyncRequest())
                .subscribe(new BaseObserver<List<SelectedBean>>() {
            @Override
            public void onResponse(BaseResult<List<SelectedBean>> listBaseResult, List<SelectedBean> items, Disposable disposable) {
                if (!Utils.isEmptyList(items)) {
                    if (callback != null) {
                        callback.onResponse(items.get(0));
                    }
                } else {
                    if (callback != null) {
                        callback.onError(Constants.ErrCode.NoData, "no data");
                    }
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }
    public void getMaleRenqi(ItemRefreshBean item, INetCommCallback<SelectedBean> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        SelectedRequestParams params = new SelectedRequestParams();
        params.type_name = item.type;
        if (item.selectedItem.page_info != null) {
            params.page = BookCityUtils.updateItemPage(item);
            params.num = Integer.parseInt(item.selectedItem.page_info.num);
        }
        mBookCityService.getMaleRenqi(params)
                .compose(asyncRequest())
                .subscribe(new BaseObserver<List<SelectedBean>>() {
            @Override
            public void onResponse(BaseResult<List<SelectedBean>> listBaseResult, List<SelectedBean> items, Disposable disposable) {
                if (!Utils.isEmptyList(items)) {
                    if (callback != null) {
                        callback.onResponse(items.get(0));
                    }
                } else {
                    if (callback != null) {
                        callback.onError(Constants.ErrCode.NoData, "no data");
                    }
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

    public void getFemaleRenqi(ItemRefreshBean item, INetCommCallback<SelectedBean> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        SelectedRequestParams params = new SelectedRequestParams();
        params.type_name = item.type;
        if (item.selectedItem.page_info != null) {
            params.page = BookCityUtils.updateItemPage(item);
            params.num = Integer.parseInt(item.selectedItem.page_info.num);
        }
        mBookCityService.getFemaleRenqi(params)
                .compose(asyncRequest())
                .subscribe(new BaseObserver<List<SelectedBean>>() {
            @Override
            public void onResponse(BaseResult<List<SelectedBean>> listBaseResult, List<SelectedBean> items, Disposable disposable) {
                if (!Utils.isEmptyList(items)) {
                    if (callback != null) {
                        callback.onResponse(items.get(0));
                    }
                } else {
                    if (callback != null) {
                        callback.onError(Constants.ErrCode.NoData, "no data");
                    }
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

    /**
     * 获取所有分类信息
     */
    public void getAllCategories() {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (mCategoryView != null) {
                mCategoryView.showError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mBookCityService.getAllCategories("0")
                .compose(asyncRequest())
                .subscribe(new BaseObserver<List<SelectedCategoryBean>>() {
            @Override
            public void onResponse(BaseResult<List<SelectedCategoryBean>> result, List<SelectedCategoryBean> selectedBeans, Disposable disposable) {
                if (mCategoryView != null && BaseResult.isNotNull(result)) {
                    if (mCategoryView != null) {
                        mCategoryView.showContent(result);
                    }
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mCategoryView != null) {
                    mCategoryView.showError(code, errDesc);
                }
            }
        });
    }

    /**
     * 获取大分类下的子分类
     * @param category_id_1
     * @param callback
     */
    public void getSubCategories(String category_id_1, INetCommCallback<CategoryBean> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mBookCityService.getSubCategories(category_id_1)
                .compose(asyncRequest())
                .subscribe(new BaseObserver<CategoryBean>() {
            @Override
            public void onResponse(BaseResult<CategoryBean> categoryBeanBaseResult, CategoryBean categoryBean, Disposable disposable) {
                if (callback != null) {
                    callback.onResponse(categoryBean);
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

    /**
     * 获取该分类下的书
     * @param req
     */
    public void getCategoryBooks(SelectedCategoryReq req) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (mBookView != null) {
                mBookView.showError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mBookCityService.getSubCategoryBooks(req).compose(asyncRequest()).subscribe(new BaseObserver<List<BookInfo>>() {
            @Override
            public void onResponse(BaseResult<List<BookInfo>> listBaseResult, List<BookInfo> selectedBeans, Disposable disposable) {
                if (mBookView != null && BaseResult.isNotNull(listBaseResult)) {
                    mBookView.showContent(listBaseResult);
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mBookView != null) {
                    mBookView.showError(code, errDesc);
                }
            }
        });
    }

    /**
     * 获取完本-经典书籍
     */
    public void getSelectFinishedBooks(PageRequestParams params, String site) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (mMvpView != null) {
                mMvpView.showError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mBookCityService.getMaleOrFemaleSelectFinished(params, site).compose(asyncRequest()).subscribe(new BaseObserver<List<SelectedBean>>() {
            @Override
            public void onResponse(BaseResult<List<SelectedBean>> listBaseResult, List<SelectedBean> selectedBeans, Disposable disposable) {
                if (mMvpView != null && BaseResult.isNotNull(listBaseResult)) {
                    mMvpView.showContent(listBaseResult);
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mMvpView != null) {
                    mMvpView.showError(code, errDesc);
                }
            }
        });
    }


    /**
     * @param params
     */
    public void getTopics(SelectedRequestParams params) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (mMvpView != null) {
                mMvpView.showError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mBookCityService.getTopics(params).compose(asyncRequest()).subscribe(new BaseObserver<List<SelectedBean>>() {
            @Override
            public void onResponse(BaseResult<List<SelectedBean>> listBaseResult, List<SelectedBean> selectedBeans, Disposable disposable) {
                if (mMvpView != null && BaseResult.isNotNull(listBaseResult)) {
                    mMvpView.showContent(listBaseResult);
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mMvpView != null) {
                    mMvpView.showError(code, errDesc);
                }
            }
        });
    }
    /**
     * 获取大分类下的子分类
     * @param params
     * @param callback
     */
    public void getTopicsBanner(TopicBannerParams params, INetCommCallback<List<BannerItem>> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mBookCityService.getTopicsBanner(params).compose(asyncRequest()).subscribe(new BaseObserver<List<BannerItem>>() {
            @Override
            public void onResponse(BaseResult<List<BannerItem>> categoryBeanBaseResult, List<BannerItem> categoryBean, Disposable disposable) {
                if (callback != null) {
                    callback.onResponse(categoryBean);
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

    /**
     * @param params
     * @param callback
     */
    public void getNewBookOrAttractBooks(PageRequestParams params, INetCommCallback<List<WrapBookInfo>> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mBookCityService.getNewBookOrAttractBooks(params).compose(asyncRequest()).subscribe(new BaseObserver<List<BookInfo>>() {
            @Override
            public void onResponse(BaseResult<List<BookInfo>> result, List<BookInfo> categoryBean, Disposable disposable) {
                if (callback != null) {
                    callback.onResponse(BookCityUtils.WrapBookInfo(params.page, categoryBean, 3, result.result.total_page));
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

    public void getBookSet(BookSetRequestParams params, INetCommCallback<BookSet> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mBookCityService.getBookSet(params).compose(asyncRequest()).subscribe(new BaseObserver<BookSet>() {
            @Override
            public void onResponse(BaseResult<BookSet> result, BookSet categoryBean, Disposable disposable) {
                if (callback != null) {
                    callback.onResponse(categoryBean);
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });

    }

    /**
     * 获取大家都在搜索
     * @param params
     * @param callback
     */
    public void getHotSearch(SelectedRequestParams params, INetCommCallback<SelectedBean> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mBookCityService.getHotLabel(params).compose(asyncRequest()).subscribe(new BaseObserver<List<SelectedBean>>() {
            @Override
            public void onResponse(BaseResult<List<SelectedBean>> listBaseResult, List<SelectedBean> items, Disposable disposable) {
                if (!Utils.isEmptyList(items)) {
                    if (callback != null) {
                        callback.onResponse(items.get(0));
                    }
                } else {
                    if (callback != null) {
                        callback.onError(Constants.ErrCode.NoData, "no data");
                    }
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

    /**
     * 搜索
     * @param params
     * @param callback
     */
    public void doSearch(SearchRequestParams params, INetCommCallback<List<BookInfo>> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
                return;
            }
            if (mBookView != null) {
                mBookView.showError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mBookCityService.doSearch(params).compose(asyncRequest()).subscribe(new BaseObserver<List<BookInfo>>() {
            @Override
            public void onResponse(BaseResult<List<BookInfo>> listBaseResult, List<BookInfo> items, Disposable disposable) {
                if (!Utils.isEmptyList(items)) {
                    if (callback != null) {
                        callback.onResponse(items);
                    }
                    if (mBookView != null) {
                        mBookView.showContent(listBaseResult);
                    }
                } else {
                    if (callback != null) {
                        callback.onError(Constants.ErrCode.NoData, "no data");
                    }
                    if (mBookView != null) {
                        mBookView.showError(Constants.ErrCode.NoData, "no data");
                    }
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
                if (mBookView != null) {
                    mBookView.showError(code, errDesc);
                }
            }
        });
    }

    public void doSearch(SearchRequestParams params) {
        doSearch(params, null);
    }


    /**
     * 获取书籍详情
     * @param bookId
     */
    public void getBookDetails(String bookId) {
        mBookCityService.getBookDetails(bookId).compose(asyncRequest()).subscribe(new BaseObserver<BookDetailsBean>() {
            @Override
            public void onResponse(BaseResult<BookDetailsBean> listBaseResult, BookDetailsBean items, Disposable disposable) {
                if (mBookDetailsView != null) {
                    mBookDetailsView.showContent(listBaseResult);
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mBookDetailsView != null) {
                    mBookDetailsView.showError(code, errDesc);
                }
            }
        });
    }

    public void getRankingList(BookListReqParams params) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (mBookView != null) {
                mBookView.showError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mBookCityService.getRankingList(params).compose(asyncRequest()).subscribe(new BaseObserver<List<BookInfo>>() {
            @Override
            public void onResponse(BaseResult<List<BookInfo>> listBaseResult, List<BookInfo> items, Disposable disposable) {
                if (!Utils.isEmptyList(items)) {
                    if (mBookView != null) {
                        mBookView.showContent(listBaseResult);
                    }
                } else {
                    if (mBookView != null) {
                        mBookView.showError(Constants.ErrCode.NoData, "no data");
                    }
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mBookView != null) {
                    mBookView.showError(code, errDesc);
                }
            }
        });
    }


    @Override
    public void detachView() {
        super.detachView();
        mSelectedView = null;
        mMvpView = null;
        mCategoryView = null;
        mBookView = null;
        mBookDetailsView = null;
    }
}
